﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using ComputerVision.Data;
using ComputerVision.Data.Repositories;

namespace ComputerVision.Services.RoleService
{
    public class RoleService : IService<aspnet_Roles>
    {
        private readonly IEntityRepository<aspnet_Roles> _roleRepository;

        public RoleService(IEntityRepository<aspnet_Roles> roleRepository)
        {
            _roleRepository = roleRepository;
        }

        public aspnet_Roles Create(aspnet_Roles t)
        {
            throw new NotImplementedException();
        }

        public aspnet_Roles Update(aspnet_Roles item)
        {
            aspnet_Roles itemDetails;
            try
            {
                itemDetails = _roleRepository.Get(item.RoleId);
                itemDetails.RoleName = item.RoleName;
                itemDetails.LoweredRoleName = item.LoweredRoleName.ToLower();
                _roleRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                string msg = string.Empty;
                foreach (DbEntityValidationResult validationErrors in dbEx.EntityValidationErrors)
                    foreach (DbValidationError validationError in validationErrors.ValidationErrors)
                        msg += Environment.NewLine +
                               string.Format("Property: {0} Error: {1}", validationError.PropertyName,
                                   validationError.ErrorMessage);

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
            return itemDetails;
        }

        public aspnet_Roles Detail(Guid id)
        {
            aspnet_Roles item = _roleRepository.Get(id);
            return item;
        }

        public aspnet_Roles Detail(int id)
        {
            aspnet_Roles item = _roleRepository.Get(id);
            return item;
        }
        
        public List<aspnet_Roles> ToList()
        {
            var item = _roleRepository.GetAll().ToList();
            return item;
        }

        public void Delete(aspnet_Roles t)
        {
            throw new NotImplementedException();
        }
    }
}